package com.zjy.portdemo;

import android.graphics.Color;
import androidx.annotation.NonNull;
import android.util.Log;
import android.widget.TextView;

import com.app.lxx.pregnancyberth.Mvp.Presenter.CurrencyPresenter;
import com.app.lxx.pregnancyberth.Mvp.View.CurrencyView;
import com.app.lxx.pregnancyberth.R;
import com.app.lxx.pregnancyberth.base.BaseActivity;
import com.app.lxx.pregnancyberth.ui.entity.SmartHomeEntity;
import com.zjy.portdemo.modbus.ModbusMaster;

import java.util.List;


/**
 * Created by Lxx on
 * 2019/11/22 17:50 星期五
 */


public class LightingActivity extends BaseActivity<CurrencyPresenter> implements CurrencyView {

    ModbusMaster modbusMaster;
    private ColorPickerView colorDisk = null;
    private TextView tv = null;
    private String address;

    @Override
    protected CurrencyPresenter createP() {
        return new CurrencyPresenter();
    }

    @Override
    protected void initBindViewId() {
        int position = getIntent().getIntExtra("position", -1);
        SmartHomeEntity smartHomeEntity = (SmartHomeEntity) getIntent().getSerializableExtra("SmartHomeEntity");
        if (smartHomeEntity != null) {
            SmartHomeEntity.DataBean data = smartHomeEntity.getData();
            if (data != null) {
                List<SmartHomeEntity.DataBean.IntelligentDataBean> intelligentData = data.getIntelligent_data();
                if (intelligentData != null) {
                    SmartHomeEntity.DataBean.IntelligentDataBean intelligentDataBean = intelligentData.get(position);
                    address = intelligentDataBean.getAddress();
                    Log.e("ssss", address);
                }
            }
        }


        colorDisk = findViewById(R.id.colorDisk);
        tv = findViewById(R.id.tv);
//        /**
//         * 初始化串口上下文、路径、波特率
//         */
//        modbusMaster = new ModbusMaster(this, "/dev/ttyS3", 115200);
//        /**
//         * 打开串口，并监听串口返回数据
//         */
//        modbusMaster.start();
//        Log.e("ssss", "打开串口");

        colorDisk.setOnColorBackListener(new ColorPickerView.OnColorBackListener() {
            @Override
            public void onColorBack(int a, int r, int g, int b) {
                tv.setText("R：" + r + "\nG：" + g + "\nB：" + b + "\n" + colorDisk.getColorStr());
                tv.setTextColor(Color.argb(a, r, g, b));

                /**
                 * 定义数组共5位
                 * 彩色灯光
                 */
                int[] data = new int[9];
                data[0] = r;//红色
                data[1] = g;//绿色
                data[2] = b;//蓝色
                data[3] = 0;//暖色:0-255
                data[4] = 0;//冷色:0-255
                data[5] = 0;//暖色dac:0-1000
                data[6] = 0;//冷色dac:0-1000
                data[7] = 0;//暖色pwm:0-1000
                data[8] = 0;//冷色pwm:0-1000
                /**
                 * 发送地址为0x01
                 * 固定位0x10
                 * 起始位为0
                 * data数据
                 */
                modbusMaster.sendDataMaster((byte) 0x01, (byte) 0x10, 0, data);
            }
        });
    }

    @Override
    protected boolean hidetitle() {
        return false;
    }

    @Override
    protected String settitle() {
        return "智能灯光";
    }

    @NonNull
    @Override
    public int setxmlview() {
        return R.layout.activity_lighting;
    }

    @Override
    public void requestCurrencyView(int type, String body) {

    }

    @Override
    public void requestCurrencyViewFailed(int type, int code) {

    }

    public String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }

}
